package com.dragon.client.controller.contract;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.IRefundService;
import com.dragon.repository.domain.dto.RefundDTO;
import com.dragon.repository.domain.dto.RefundProductDTO;
import com.dragon.repository.domain.vo.RefundProductVO;
import com.dragon.repository.domain.vo.RefundVO;
import com.dragon.repository.entity.Refund;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 退款表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2024-01-09
 */
@RestController
@RequestMapping("refund")
@Api(tags = "退款管理")
public class RefundController {

    @Autowired
    IRefundService service;



    @GetMapping("pageList")
    @ApiOperation(value = "分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractCode",required = false,type = "string",value = "合同编码"),
            @ApiImplicitParam(name = "customerName",required = false,type = "string",value = "客户名称"),
            @ApiImplicitParam(name = "startTime",required = false,type = "string",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",required = false,type = "string",value = "结束时间"),
            @ApiImplicitParam(name = "status",required = false,type = "string",value = "退款状态"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<RefundDTO>> selectListPage(
            @RequestParam(required = false) String contractCode,
            @RequestParam(required = false) String customerName,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer status,
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Refund> refundPage = service.selectPageList(contractCode, customerName, startTime, endTime, status, page, limit);

        return ResponseData.success(RefundDTO.convertList(refundPage.getRecords()),refundPage.getTotal());

    }

    @GetMapping("detail")
    @ApiOperation(value = "详情")
    public ResponseData<RefundDTO> selectDetailById(@ApiParam(name = "id",required = true,type = "int",value = "主表id") @RequestParam Integer id){
        Refund refund = service.selectDetailById(id);
        RefundDTO refundDTO = RefundDTO.convert(refund);
        refundDTO.setRefundProducts(RefundProductDTO.convertList(refund.getRefundProducts()));
        return ResponseData.success(refundDTO);
    }


    @PostMapping("add")
    @ApiOperation(value = "添加退款")
    @AddPage
    public ResponseData addRefund(@RequestBody @Validated(Insert.class)RefundVO vo){
        Refund refund = RefundVO.convert(vo);
        refund.setRefundProducts(RefundProductVO.convertList(vo.getRefundProducts()));
        return ResponseData.success(service.addRefund(refund));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改退款")
    public ResponseData updateRefund(@RequestBody @Validated(Update.class) RefundVO vo){
        Refund refund = RefundVO.convert(vo);
        refund.setRefundProducts(RefundProductVO.convertList(vo.getRefundProducts()));
        service.updateRefund(refund);
        return ResponseData.success();
    }

    @PostMapping("updateStatus")
    @ApiOperation(value = "作废或者删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,type = "int",value = "主表id"),
            @ApiImplicitParam(name = "status",required = true,type = "int",value = "退款状态:3作废，4删除")
    })
    public ResponseData deleteRefund(@RequestParam Integer id,@RequestParam Integer status){
        service.updateStatus(id,status);
        return ResponseData.success();
    }



}
